package cn.elwy.rcp.ui.explorer.create.wizard;

import java.io.File;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWizard;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.elwy.rcp.core.model.ui.Condition;
import cn.elwy.rcp.core.model.ui.progress.ProgressMonitor;
import cn.elwy.rcp.ui.dialog.MsgDialog;
import cn.elwy.rcp.ui.explorer.core.AppContext;
import cn.elwy.rcp.ui.explorer.core.ProjectConst;
import cn.elwy.rcp.ui.explorer.core.model.Operation;
import cn.elwy.rcp.ui.explorer.core.service.ProjectConfigService;
import cn.elwy.rcp.ui.explorer.core.vo.ProjectConfig;
import cn.elwy.rcp.ui.explorer.create.NewProjectModel;
import cn.elwy.rcp.ui.explorer.project.ProjectUtil;
import cn.elwy.rcp.ui.wizard.BaseWizard;

public class NewBuilderProjectWizard extends BaseWizard implements IWorkbenchWizard {

	public static final String ID = "NewBuilderProjectWizard";

	private Logger logger = LoggerFactory.getLogger(NewBuilderProjectWizard.class);

	private ProjectConfigService service = AppContext.getService(ProjectConfigService.ID, ProjectConfigService.class);

	private ProjectConfig projectConfig;

	private ProjectInfoWizardPage projectInfoWizardPage;
	private ProjectSvnWizardPage projectSvnWizardPage;
	private ReleaseWizardPage releaseWizardPage;

	public NewBuilderProjectWizard() {
		this("新建构建工程", Operation.NEW);
	}

	public NewBuilderProjectWizard(String title, Operation operation) {
		// try{
		//
		// }catch (SVNException e) {
		// // TODO: handle exception
		// }
		setWindowTitle(title);
		Condition condition = new Condition();
		condition.put(NewProjectModel.OPERATION, operation);
		condition.put(NewProjectModel.FILE_PATH, null);

		NewProjectModel newProjectModel = new NewProjectModel(condition);
		projectConfig = newProjectModel.getInput();
		projectInfoWizardPage = new ProjectInfoWizardPage();
		projectSvnWizardPage = new ProjectSvnWizardPage();
		releaseWizardPage = new ReleaseWizardPage();
	}

	@Override
	public void addPages() {
		addPage(projectInfoWizardPage);
		addPage(projectSvnWizardPage);
		addPage(releaseWizardPage);
	}

	@Override
	public boolean canFinish() {
		return super.canFinish() && this.releaseWizardPage.getControl() != null;
	}

	@Override
	public boolean performFinish() {
		if (!super.performFinish() || !testLoginSvn()) {
			return false;
		}

		String projectPath = projectConfig.getProjectPath().replace("\\", "/");
		String projectName = projectConfig.getProjectName() + "_" + projectConfig.getVersion();

		String osString = Platform.getLocation().toOSString().replace("\\", "/");
		File file = null;
		file = new File(projectPath, ProjectConst.PACKER_FILE_NAME);
		if (projectPath.equals(osString + "/" + projectName)) {
			projectPath = osString;
			file = new File(projectPath, projectName + "/" + ProjectConst.PACKER_FILE_NAME);
		} else {
		}
		final IPath location = new Path(projectPath);
		service.save(file, projectConfig);

		try {
			IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
			ProjectUtil.createProject(project, location);

			service.saveLastProject(projectConfig);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			MsgDialog.open(getShell(), e);
			return false;
		}
	}

	public boolean testLoginSvn() {
		boolean testLoginSvn = projectSvnWizardPage.testLoginSvn(new ProgressMonitor(null));
		if (!testLoginSvn) {
			getContainer().showPage(projectSvnWizardPage);
		}
		return testLoginSvn;
	}

	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
	}

	public ProjectConfig getProjectConfig() {
		return projectConfig;
	}

}
